package src.World;

import java.io.*;
import src.Main;
import src.Players.Player;

public class Shops
{
    public int maxShops = 31; // Number + 1.
    public int maxShopItems = 101;
    private int maxInShopItems = 40;
    private int maxShowDelay = 30;
    public int totalShops = 0;
    public ShopList[] shopLists;

    public Shops()
    {
        shopLists = new ShopList[maxShops];
        loadShops("SHOPS.CFG");
    }

    public void process()
    {
        boolean didUpdate = false;
        for(ShopList sl : shopLists)
        {
            if(sl == null)
            {
                continue;
            }
            for(int j = 0; j < maxShopItems; j++)
            {
                if(sl.getShopItems()[j] > 0)
                {
                    if(sl.getShopItemsDelay()[j] >= maxShowDelay)
                    {
                        if(j <= sl.shopItemsStandard && sl.getShopItemsN()[j] <= sl.getShopItemsSN()[j])
                        {
                            if(sl.getShopItemsN()[j] < sl.getShopItemsSN()[j])
                            {
                                sl.setShopItemsN(j, 1);
                            }
                        }
                    }
                    else
                    {
                        discountItem(sl, j);
                    }
                    sl.resetShopDelay(j);
                    didUpdate = true;
                }
                sl.increaseShopDelay(j);
            }
        }
        if(didUpdate)
        {
            for(Player p : Main.m.ge.players)
            {
                if(p != null)
                {
                    if(p.myShopId != 0)
                    {
                        p.updateShop = true;
                    }
                }
            }
        }
    }

    public void discountItem(ShopList sl, int arrayId)
    {
        if(sl == null)
        {
            return;
        }
        if(sl.getShopItemsN()[arrayId] > 0)
        {
            sl.setShopItemsN(arrayId, -1);
        }
        else if(sl.getShopItemsN()[arrayId] == 0)
        {
            sl.removeShopItem(arrayId);
        }
    }

    public boolean loadShops(String fileName)
    {
        String line = "", token = "", token2 = "", token2_2 = "", token3[] = new String[(maxShopItems * 2)];
        BufferedReader shopFile = null;
        try
        {
            shopFile = new BufferedReader(new FileReader("./src/Data/CFG/" + fileName));
            line = shopFile.readLine().trim();
        }
        catch(Exception e)
        {
            Main.m.log("Error loading shop list");
            return false;
        }
        while (line != null)
        {
            int spot = line.indexOf("=");
            if(spot > -1)
            {
                token = line.substring(0, spot).trim();
                token2 = line.substring(spot + 1).trim();
                token2_2 = token2.replaceAll("\t\t", "\t");
                token2_2 = token2_2.replaceAll("\t\t", "\t");
                token3 = token2_2.split("\t");
                if(token.equals("shop"))
                {
                    int ShopID = Integer.parseInt(token3[0]);
                    String ShopName = token3[1].replaceAll("_", " ");
                    int ShopSModifier = Integer.parseInt(token3[2]);
                    int ShopBModifier = Integer.parseInt(token3[3]), ShopItemsStandard = 0;
                    int[] ShopItems = new int[101], ShopItemsN = new int[101], ShopItemsSN = new int[101];
                    for(int i = 0; i < ((token3.length - 4) / 2); i++)
                    {
                        if(token3[(4 + (i * 2))] != null)
                        {
                            ShopItems[i] = (Integer.parseInt(token3[(4 + (i * 2))]) + 1);
                            ShopItemsN[i] = Integer.parseInt(token3[(5 + (i * 2))]);
                            ShopItemsSN[i] = Integer.parseInt(token3[(5 + (i * 2))]);
                            ShopItemsStandard++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    shopLists[ShopID] = new ShopList(ShopName, ShopSModifier, ShopBModifier, ShopItemsStandard, ShopItems, ShopItemsN, ShopItemsSN);
                    totalShops++;
                }
            }
            else
            {
                if(line.equals("[ENDOFSHOPLIST]"))
                {
                    try
                    {
                        shopFile.close();
                    }
                    catch(IOException ioexception)
                    {}
                    shopFile = null;
                    return true;
                }
            }
            try
            {
                line = shopFile.readLine().trim();
            }
            catch(IOException ioexception1)
            {
                try
                {
                   shopFile.close();
                }
                catch(IOException ioexception)
                {}
                shopFile = null;
                return true;
            }
        }
        return false;
    }
}
